import React, { Component } from 'react';
import { Icon } from 'antd';
import message from '@wanhu/antd-legacy/lib/message'
import {callOut,themeShow} from '../header/customer/customerApi'
import connectRouter from '../../mixins/router';
import api from '../../api/api';
import context from '../../api/contextCreator'

class DialButton extends Component {
    state = {
        searchType: 'customer',
        showBox:false,
        callInfo:null,
        haveAgientId:false,
    }
    constructor(props){
        super(props)
        this.getTaskInfoData = null
        this.getTaskRecordsData = null
    }
    async componentDidMount() {
        const { router } = this.props;
        const { phone, address, name, theme, patinetId} = this.props
        const match = router.match('/taskDetail/:taskId');
        if (match && match.taskId) {
            const getTaskInfo = await api.getTaskById(match.taskId)
            this.getTaskInfoData = getTaskInfo;
            const getTaskRecords = await api.getTaskRecords(match.taskId)
            this.getTaskRecordsData = getTaskRecords
        }
        const userInfo = await api.getUserInfo()
        const getAgentIdNum = await api.getAgentId(userInfo.user)
        if(getAgentIdNum){
            this.setState({
                haveAgientId:true
            })
        }

    }
    async isNewMemberFun(phone){
        // 判断是否为新会员
        let params = {
            where: {
                phone:phone
            },
            skip: 0,
            limit: 1,
            count: 1
        }
        const isNewMember = await api.searchPatientCount(params)
        return isNewMember
    }
    callNew =async (e) =>  {
        let getCallOnOff = localStorage.getItem("callOnOff");
        if(getCallOnOff === "off"){
            message.error("坐席忙碌或者离线，请调整坐席状态后再拨打电话。");
            return;
        }
        if(!this.state.haveAgientId){
            message.error("未绑定坐席账号或坐席忙碌，无法发起外呼");
            return;
        }
        let taskId = "",taskType="",taskReclassify="",subject="",content = "";
        if(this.getTaskInfoData && this.getTaskInfoData.id){
            taskId = this.getTaskInfoData.id
            taskType = this.getTaskInfoData.taskType
            taskReclassify = this.getTaskInfoData.taskReclassify
            subject = this.getTaskInfoData.content
            let item = []
            if (this.getTaskRecordsData && this.getTaskRecordsData.length > 0) {
                for (let i in this.getTaskRecordsData) {
                    // console.log(this.getTaskRecordsData,this.getTaskRecordsData[i])
                    item.push(this.getTaskRecordsData[i].createDate+":"+this.getTaskRecordsData[i].content)
                }
            }
            content = item.join("");
        }
        const { phone, address, name, theme, patinetId} = this.props
        if(this.props.theme){
            // 非任务界面发起 theme:true任务界面发起电话条
            themeShow(phone,address,name,theme,patinetId,"","","","","")
        }else{
            // 任务界面发起 theme:false非任务界面发起电话条
            callOut(phone,address,name,theme,patinetId,taskId,taskType,taskReclassify,subject,content)
        }
        return
    }
    render() {
        return <a onClick={this.callNew}><Icon type="phone" /></a>
    }
}

// export default DialButton;
export default connectRouter(DialButton);
